﻿using System;
using System.Collections.Generic;

using ComponentFactory.Krypton.Toolkit;

using ExtensibleEventViewer.Console.EventArguments;
using ExtensibleEventViewer.Console.Presenters;
using ExtensibleEventViewer.Console.Views;
using ExtensibleEventViewer.Core.Interfaces;


namespace ExtensibleEventViewer.Console.Controls
{
	public partial class EventLogProviderListFilter : DecoratedUserControl, IEventLogProviderListView
	{
		private readonly EventLogProviderListPresenter _presenter;

		private IEventLogProvider _lastSelectedProvider;

		/// <summary>
		/// Occurs after event log provider was changed
		/// </summary>
		public event EventHandler<SelectedProviderChangedEventArgs> SelectedProviderChanged;

		/// <summary>
		/// Initializes the control
		/// </summary>
		public EventLogProviderListFilter()
		{
			InitializeComponent();

			_presenter = new EventLogProviderListPresenter(this);

			ddlEventLogProviderList.SelectedValueChanged += SelectedValueChanged;
		}

		protected override void OnLoad(EventArgs e)
		{
			if (!DesignMode)
			{
				_presenter.LoadData();
			}

			base.OnLoad(e);
		}

		private void SelectedValueChanged(object sender, EventArgs e)
		{
			if (!Equals(_lastSelectedProvider, ddlEventLogProviderList.SelectedItem as IEventLogProvider))
			{
				_lastSelectedProvider = ddlEventLogProviderList.SelectedItem as IEventLogProvider;

				if (SelectedProviderChanged != null)
				{
					SelectedProviderChangedEventArgs eventArgs = new SelectedProviderChangedEventArgs
					                                             	{
					                                             		EventLogProvider =
					                                             			ddlEventLogProviderList.SelectedItem as IEventLogProvider
					                                             	};

					SelectedProviderChanged(sender, eventArgs);
				}
			}
		}

		public IEnumerable<IEventLogProvider> Providers
		{
			set
			{
				if (ddlEventLogProviderList.InvokeRequired)
				{
					Action populateProviderList = () => PopulateProviderList(value);

					ddlEventLogProviderList.Invoke(populateProviderList);
				}
				else
				{
					PopulateProviderList(value);
				}
			}
		}

		public IEventLogProvider SelectedEventLogProvider
		{
			get
			{
				return ddlEventLogProviderList.SelectedItem as IEventLogProvider;
			}
		}

		private void PopulateProviderList(IEnumerable<IEventLogProvider> providers)
		{
			ddlEventLogProviderList.BeginUpdate();

			ddlEventLogProviderList.Items.Clear();

			foreach (IEventLogProvider provider in providers)
			{
				ddlEventLogProviderList.Items.Add(provider);
			}

			if (ddlEventLogProviderList.Items.Count > 0)
			{
				ddlEventLogProviderList.SelectedIndex = 0;
			}

			ddlEventLogProviderList.EndUpdate();
		}

		public override PaletteMode PaletteMode
		{
			set
			{
				ddlEventLogProviderList.PaletteMode = value;
			}
		}
	}
}
